﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.DAL;
using System.Collections.Generic;
using _WebShared.Classes;
using KPIS.GERP.GHRMS.APP.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class TR_LecturerSkills : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        private ITrainingLecturersBLL LecturersBLL = new TrainingLecturersBLL();
        private ITrainingLecturerSkillsBLL LecSkillsBLL = new TrainingLecturerSkillsBLL();
        InitialData_BLL iniData = new InitialData_BLL();
        private Main main = new Main();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadGrid();
                //lblViewby.Text = "***จำแนกตามวิทยากร";
            }
        }
        private void LoadGrid()
        {
            grdTRLecturerSkill.DataSource = LecSkillsBLL.GetFull();
            grdTRLecturerSkill.DataBind();
            RadGrid1.DataSource = LecSkillsBLL.GetFull();
            grdTRLecturerSkill.DataBind();
        }
        protected void grdTRLecturerSkill_ItemCommand(object o, GridCommandEventArgs e)
        {
            if (e.CommandName == "AddDataSubject")
            {
                UpdateUI("AddNew");

            }
            
            else if (e.CommandName == "Delete")
            {
                string TrainerCapSeq = grdTRLecturerSkill.Items[e.Item.ItemIndex]["training_cap_seq"].Text;
                int UserID = ssMng.EmpSeqI;
                DateTime dtNow = DateTime.Now;

                TrainingLecturerSkillsInfo info = new TrainingLecturerSkillsInfo();
                info.training_cap_seq = Convert.ToInt32(TrainerCapSeq);
                info.updated_by = UserID;
                info.updated_when = dtNow;

                LecSkillsBLL.Delete(info);
            }
            else if (e.CommandName == "GridEdit")
            {
                UpdateUI("Edit");

                hdfTrainerCapSeq.Value = grdTRLecturerSkill.Items[e.Item.ItemIndex]["training_cap_seq"].Text;

                GridDataItem item = (GridDataItem)e.Item;
                cbLecturer.SelectedValue = main.WebString(item["trainer_seq"].Text);
                cbSubject.SelectedValue = main.WebString(item["training_subject_seq"].Text);
            }
            LoadGrid();
        }
        
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (checkDuplicate() == true)
            {
                int TRTrainer = Convert.ToInt32(cbLecturer.SelectedValue);
                int TRSubject = Convert.ToInt32(cbSubject.SelectedValue);
                //int UserID = ssMng.EmpSeqI;
                int UserID = 70;
                DateTime dtNow = DateTime.Now;
                UpdateUI("Save");
                ClearControl();
                TrainingLecturerSkillsInfo info = new TrainingLecturerSkillsInfo();
                info.trainer_seq = TRTrainer;
                info.training_subject_seq = TRSubject;
                info.created_by = UserID;
                info.created_when = dtNow;

                LecSkillsBLL.Insert(info);

                LoadGrid();
            }
            else
            {
                MessageX.Alert(Page, "วิทยากรและรายวิชาที่เลือกมีในระบบแล้ว กรุณาเลือกข้อมูลใหม่");
            }
            
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ClearControl();
            UpdateUI("Save");
        }
        protected void LoadTrainer()
        {
            cbLecturer.DataSource = LecturersBLL.GetFull();
            cbLecturer.DataBind();
        }
        protected void LoadTrainingSubject()
        {
            cbSubject.DataSource = iniData.ListTrainingSubject("");
            cbSubject.DataBind();
        }
        protected void ClearControl()
        {
            cbSubject.Items.Clear();
            cbSubject.Text = "";
            cbLecturer.Items.Clear();
            cbLecturer.Text = "";           
        }
        protected void UpdateUI(string strCommand)
        {
            switch (strCommand)
            {                
                case "Save":
                    tblTRLecturerSkill.Visible = true;
                    LecturerSkillEdit.Visible = false;                 
                    
                    break;

                case "AddNew":
                    tblTRLecturerSkill.Visible = false;
                    LecturerSkillEdit.Visible = true;
                    btnUpdate.Visible = false;
                    LoadTrainer();
                    LoadTrainingSubject();

                    break;

                case "Edit":
                    btnUpdate.Visible = true;
                    btnSave.Visible = false;
                    LecturerSkillEdit.Visible = true;
                    LoadTrainer();
                    LoadGrid();
                    LoadTrainingSubject();
                    
                    break;
            }
        }
        protected bool checkDuplicate()
        {
            bool bOK = true;
            DataSet CheckDataCommand = LecSkillsBLL.CheckDuplicate(cbLecturer.SelectedValue, cbSubject.SelectedValue);
            DataTable table = CheckDataCommand.Tables[0];
            if (table.Rows.Count > 0)
            {
                bOK = false;
            }

            return bOK;
        }

        //================================= จำแนกตามรายวิชา =============================
        protected void RadGrid1_ItemCommand(object o, GridCommandEventArgs e)
        {
            if (e.CommandName == "AddDataTrainer")
            {
                UpdateUI2("AddNew");
            }
            
            else if (e.CommandName == "DeleteTrainer")
            {
                string TrainerCapSeq = RadGrid1.Items[e.Item.ItemIndex]["training_cap_seq"].Text;
                int UserID = ssMng.EmpSeqI;
                DateTime dtNow = DateTime.Now;
                TrainingLecturerSkillsInfo info = new TrainingLecturerSkillsInfo();
                info.training_cap_seq = Convert.ToInt32(TrainerCapSeq);
                info.updated_by = UserID;
                info.updated_when = dtNow;
                LecSkillsBLL.Delete(info);
                UpdateUI2("Save");
            }

            LoadGrid();
        }
        protected void btnSaveTrainer_Click(object sender, EventArgs e)
        {
            if (checkDuplicate2() == true)
            {
                int TRTrainer = Convert.ToInt32(RadComboBox1.SelectedValue);
                int TRSubject = Convert.ToInt32(RadComboBox2.SelectedValue);
                //int UserID = ssMng.EmpSeqI;
                int UserID = 70;
                DateTime dtNow = DateTime.Now;
                TrainingLecturerSkillsInfo info = new TrainingLecturerSkillsInfo();
                info.trainer_seq = TRTrainer;
                info.training_subject_seq = TRSubject;
                info.created_by = UserID;
                info.created_when = dtNow;

                LecSkillsBLL.Insert(info);
                UpdateUI2("Save");
                LoadGrid();
            }
            else
            {
                MessageX.Alert(Page, "วิทยากรและรายวิชาที่เลือกมีในระบบแล้ว กรุณาเลือกข้อมูลใหม่");
            }
        }
        protected void btnCancelTrainer_Click(object sender, EventArgs e)
        {
            UpdateUI2("Save");
            LoadGrid();
        }
        protected void LoadTrainer2()
        {
            RadComboBox1.DataSource = LecturersBLL.GetFull();
            RadComboBox1.DataBind();
        }
        protected void LoadTrainingSubject2()
        {
            RadComboBox2.DataSource = iniData.ListTrainingSubject("");
            RadComboBox2.DataBind();
        }
        protected bool checkDuplicate2()
        {
            bool bOK = true;
            DataSet CheckDataCommand = LecSkillsBLL.CheckDuplicate(RadComboBox1.SelectedValue, RadComboBox2.SelectedValue);
            DataTable table = CheckDataCommand.Tables[0];
            if (table.Rows.Count > 0)
            {
                bOK = false;
            }

            return bOK;
        }
        protected void UpdateUI2(string strCommand)
        {
            switch (strCommand)
            {
                case "Save":
                    PanelRadGrid1.Visible = true;
                    PanelAddData.Visible = false;

                    break;

                case "AddNew":
                    PanelRadGrid1.Visible = false;
                    PanelAddData.Visible = true;
                    LoadTrainer2();
                    LoadTrainingSubject2();

                    break;
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string strTranSeq = cbLecturer.SelectedValue;
            string TranSubSeq = cbSubject.SelectedValue;
            string TrainerCapSeq = hdfTrainerCapSeq.Value;
            if (TrainerCapSeq != "")
            {
                LecSkillsBLL.Update(strTranSeq, TranSubSeq, TrainerCapSeq);
            }
            LecturerSkillEdit.Visible = false;
            ClearControl();
            LoadGrid();
        }
    }
}
